<!DOCTYPE html> 
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0, user-scalable=0" name="viewport" />        
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" /> 
<meta content="telephone=no" name="format-detection" /> 
<title>选择部门</title>
<link rel="stylesheet" href="css/searchDep.css">
</head>
<body>
<div class="container">
	<div class="search-bar">
		<div class="search-inner">
			<img src="images/search1@2x.png" alt="">
			<input type="text" placeholder="搜索营业部门">
		</div>
	</div>
	<div class="results">
		<!-- <div class="r-list">我就是股神</div>
		<div class="r-list">我就是股神</div>
		<div class="r-list">我就是股神</div> -->
		<!-- <div class="r-list">我就是是<i>股神</i></div> -->
		
	</div>
	<div class="empty hidden">
		<img src="images/info_icon@2x.png" alt="">
		<p>无匹配结果</p>
	</div>
</div>
<script src="js/build/lib.js" type="text/javascript"></script>
<script>
var searchDep = {
	init: function(){
		// 兼容按压效果
		document.body.addEventListener('touchstart', function () { //...空函数即可
		});
	},
	events: function(){
		var _searchVal;
		$('.search-bar input').on('input',function(){
			var _searchText = _searchVal = $.trim($(this).val());
			console.log(_searchText);

			$.ajax({
				type: 'get',
				data: {
					txt: _searchText
				},
				url: "json/f.json",
				success: function(data,textStatus, jqXHR){
					// console.log(textStatus, jqXHR);
					console.log(data.menbers);
					if((data.menbers.length >0) && (data!=null)){
						var _str = '';
						for(var i=0; i<data.menbers.length; i++){
							_str += '<div class="r-list" partId="'+data.menbers[i].id+'">'+data.menbers[i].txt+'</div>';
						}
						$('.results').empty().append(_str);

						// 替换选择的字符
						for(var i=0; i<$('.r-list').size(); i++){
							var _str = $.trim($('.r-list').eq(i).text());
							var _newStr=_str.replace(_searchVal,"<i>"+_searchVal+"</i>");
							$('.r-list').eq(i).html(_newStr);
						}

					}else{
						$('.empty').show();
					}

				},
				error: function(){
					console.log(this);
				},
				complete: function(){
					console.log('complete');
				},
				beforeSend: function(){
					console.log("before");
				}

			});

			
		});
		$('.results').delegate('.r-list','click',function(){
			var _partId = $(this).attr("partId"),
				_partName = $(this).text();
			console.log(_partId,_partName);
			// 将选择的部门放到sesseion里面
			sessionStorage.setItem("partId", _partId); 
			sessionStorage.setItem("partName", _partName); 

			setTimeout(function(){
				window.location = 'select.html';
			},320);
		});


	},
	// replaceFun: function(str){

	// },
	run: function(){
		this.init();
		this.events();
	}
};
searchDep.run();
</script>


</body>
</html>